package com.ruoyi.salesInvoice.invoiceStatusMachine.impl;

import com.ruoyi.audit.domain.Audit;
import com.ruoyi.audit.mapper.AuditMapper;
import com.ruoyi.sale.domain.SaleGoodsModel;
import com.ruoyi.sale.mapper.SaleGoodsMapper;
import com.ruoyi.salesInvoice.domain.SalesInvoiceModel;
import com.ruoyi.salesInvoice.domain.SalesInvoiceOrder;
import com.ruoyi.salesInvoice.invoiceStatusMachine.InvoiceService;
import com.ruoyi.salesInvoice.mapper.SalesInvoiceMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * 发票状态 回退
 * @Author Mr.Yi.Zhang
 * @Version 1.0
 */
@Component
public class InvoiceServiceRollback implements InvoiceService {

    private static final String FLAG = "0";

    @Override
    public String get() {
        return FLAG;
    }
    
    
    /** 发票Mapper */
    private SalesInvoiceMapper salesInvoiceMapper;
    /** 销售审核Mapper */
    private AuditMapper auditMapper;
    /** 销货单Mapper */
    private SaleGoodsMapper saleGoodsMapper;
    public InvoiceServiceRollback(
            SalesInvoiceMapper salesInvoiceMapper,
            AuditMapper auditMapper,
            SaleGoodsMapper saleGoodsMapper
    ) {
        this.salesInvoiceMapper = salesInvoiceMapper;
        this.auditMapper = auditMapper;
        this.saleGoodsMapper = saleGoodsMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int execute(SalesInvoiceOrder order) {
        // 当前发票订单
        SalesInvoiceOrder salesInvoiceOrder = salesInvoiceMapper.selectSalesInvoiceOrderById(order.getSalesInvoiceId());

        /// =============同步销售审核状态=============== ///
        Audit auditQueryParams = new Audit();
        auditQueryParams.setOrderId(order.getSalesInvoiceId());
        auditQueryParams.setOrderType("7");
        Audit audit = auditMapper.selectAuditByOrderId(auditQueryParams);
        audit.setAuditStatus("0");
        auditMapper.updateAudit(audit);

        /// =============关闭是否已经开票=============== ///
        // 获取发票关联产品列表的销货单ID集合
        List<SalesInvoiceModel> salesInvoiceModels = salesInvoiceOrder.getSalesInvoiceModelList();
        List<Long> salesInvoiceModelIds = salesInvoiceModels.stream()
                .map(item -> item.getSaleGoodsModelId())
                .collect(Collectors.toList());
        // 获取销货单关联的产品的产品ID集合
        List<SaleGoodsModel> saleGoodsModels = saleGoodsMapper.selectSaleGoodsModelByModelIdList(salesInvoiceModelIds);
        List<Long> saleGoodsIds = saleGoodsModels.stream()
                .map(item -> item.getSaleGoodsId())
                .collect(Collectors.toList());
        //获取新的list集合，去重
        List<Long> saleGoodsIdsNew = new ArrayList<Long>(new TreeSet<Long>(saleGoodsIds));
        saleGoodsMapper.updateSaleorderOrderIsPayes(saleGoodsIdsNew);

        /// =============同步发票订单状态=============== ///
        salesInvoiceOrder.setAuditTime(new Date());
        salesInvoiceOrder.setInvoiceStatus(order.getInvoiceStatus());
        // 修改发票订单状态
        return salesInvoiceMapper.updateSalesInvoiceOrder(salesInvoiceOrder);
    }
}
